import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.PubBean;

import util.DBConnection;

/**
 * This servlet is used to handle requests for updating the pub's information.
 * 
 * @author Josef Hardi
 *
 */
public class UpdatePub extends HttpServlet {
	
	/** Database connection **/
	private Connection conn = null;
	
	/** Web session **/
	private HttpSession session;
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException {
	
		request.setAttribute("latitude", request.getParameter("latitude"));  
		request.setAttribute("longitude", request.getParameter("longitude"));
        RequestDispatcher rd = request.getRequestDispatcher("edit.jsp");  
        rd.forward(request,response);  
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
    	throws ServletException,IOException {
		
		// Get the database connection
		conn = DBConnection.getConnection();
		
		// Put the pub information
		PubBean pub = new PubBean();
		pub.setName(request.getParameter("txt_name"));
		pub.setDescription(request.getParameter("txt_description"));
		pub.setFacility(request.getParameter("opt_beer"));
		pub.setFacility(request.getParameter("opt_livemusic"));
		pub.setFacility(request.getParameter("opt_dancefloor"));
		pub.setFacility(request.getParameter("opt_nosmoking"));
		pub.setLatitude((int) (Double.parseDouble(request.getParameter("txt_latitude")) * 1E6));
		pub.setLongitude((int) (Double.parseDouble(request.getParameter("txt_longitude")) * 1E6));
		pub.setOpenHour(request.getParameter("txt_openhour"));
		pub.setOpenMinute(request.getParameter("txt_openminute"));
		pub.setCloseHour(request.getParameter("txt_closehour"));
		pub.setCloseMinute(request.getParameter("txt_closeminute"));
		pub.setAddress(request.getParameter("txt_address"));	
		
		PreparedStatement stmt = null;
		
  	  	try {
  	  		String query = "UPDATE Pub " +
  	  				       "SET name = ?, description = ?, " +
  	  				       "facility = ?, latitude = ?, longitude = ?, " +
  	  				       "open_hour = ?, close_hour = ?, address = ? " +
  	  				       "WHERE id = ?";
  	  		
	  	  	stmt = conn.prepareStatement(query);
			
	  	  	// Put the pub information
			stmt.setString(1, pub.getName());
			stmt.setString(2, pub.getDescription());
			stmt.setString(3, pub.getFacility());
			stmt.setInt(4, pub.getLatitude());
			stmt.setInt(5, pub.getLongitude());			
			stmt.setTime(6, pub.getOpenTime());
			stmt.setTime(7, pub.getCloseTime());
			stmt.setString(8, pub.getAddress());
			stmt.setInt(9, Integer.parseInt(request.getParameter("id")));
			
			// Execute update operation
			int rowAffected = stmt.executeUpdate();
			
			// Send message back to the page
			request.setAttribute("dbmessage", rowAffected + " record updated!"); 
            RequestDispatcher rd=request.getRequestDispatcher("browse.jsp");  
            rd.forward(request, response);	
  	  	}
  	  	catch (SQLException e) {
  	  		e.printStackTrace();
  	  	}
  	  	finally {
	    	try {
	           // Close everything.
	           if (stmt!= null) stmt.close();
	           if (conn!= null) conn.close();
	    	} 
	    	catch (Exception e) { // Trap all other exceptions
	    			e.printStackTrace();
	    	}
	    }	
	}
}
